function showPasswordModal() {
    document.getElementById('passwordModal').style.display = 'block';
}

function showEditInfoModal() {
    document.getElementById('editInfoModal').style.display = 'block';
}

function closeModal(modalId) {
    document.getElementById(modalId).style.display = 'none';
}

// 密码修改表单
const passwordForm = document.getElementById('passwordForm');
if (passwordForm) {
    passwordForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const password = this.password.value;
        const confirmPassword = this.confirmPassword.value;
        
        if (password !== confirmPassword) {
            alert('两次输入的密码不一致');
            return;
        }
        
        if (password.length < 6) {
            alert('密码长度不能少于6位');
            return;
        }
        
        const formData = new URLSearchParams();
        formData.append('password', password);
        
        fetch('/user/profile/update', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: formData.toString()
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('密码修改成功');
                closeModal('passwordModal');
                this.reset();
            } else {
                alert(data.message || '密码修改失败');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('密码修改失败，请重试');
        });
    });
}

// 头像上传
const avatarInput = document.getElementById('avatarInput');
if (avatarInput) {
    avatarInput.addEventListener('change', function() {
        if (!this.files || !this.files[0]) return;
        
        const formData = new FormData();
        formData.append('avatar', this.files[0]);
        
        fetch('/user/profile/avatar', {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                document.getElementById('userAvatar').src = data.avatar;
            } else {
                alert(data.message || '头像上传失败');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('头像上传失败，请重试');
        });
    });
}

// 资料编辑表单
const editInfoForm = document.getElementById('editInfoForm');
if (editInfoForm) {
    editInfoForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const formData = new URLSearchParams();
        formData.append('nickname', this.nickname.value);
        formData.append('email', this.email.value || '');
        formData.append('bio', this.bio.value || '');
        formData.append('gender', this.gender.value);
        
        fetch('/user/profile/info', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: formData.toString()
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('资料更新成功');
                location.reload();
            } else {
                alert(data.message || '资料更新失败');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('资料更新失败，请重试');
        });
    });
} 